import 'package:permission_handler/permission_handler.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:regular_assess/helper/global/user/logic.dart';

import '../model/award_model.dart';

import '../../../helper/utils/util.dart';
import 'index.dart';

class HomeController extends GetxController {
  HomeController();

  final state = HomeState();

  Future<void> requestForData() async {
    var res = await HomeApi.wanjian();
    List? list = res.json['list'];
    state.list = list?.map((e) => AwardModel.fromJson(e)).toList() ?? [];
    update();
  }

  void goUrl(AwardModel md) async {
    UserController.instance.md = md;

    ///清除用户信息
    // UserController.instance.loginOutSuccess();
    await UserController.instance.initData();
    if (UserController.instance.isLogin) {
      toWeb();
    } else {
      Get.toNamed(RouterPath.login);
    }
  }

  void toWeb() {
    UserController.instance.toWeb();
  }

  void onRefresh() async {
    await requestForData();
    if (state.rc.isRefresh) {
      state.rc.refreshCompleted();
    }
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.rc = RefreshController();
    // You can request multiple permissions at once.
    [
      Permission.mediaLibrary,
      Permission.audio,
      Permission.microphone,
      Permission.storage,
    ].request();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
    requestForData();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
